<template>
  <a-modal
    title="编辑表单"
    width="40%"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleSubmit"
    @cancel="
      () => {
        this.visible = false
      }
    "
  >
    <a-spin :spinning="confirmLoading">
      <a-form-model ref="form" :model="entity" :rules="rules" v-bind="layout">
        <a-form-model-item label="班次编号" prop="WorkingTimesID">
          <a-input v-model="entity.WorkingTimesID" autocomplete="off" :disabled="true" />
        </a-form-model-item>
        <a-form-model-item label="班次名称" prop="WorkingTimesName">
          <a-input v-model="entity.WorkingTimesName" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="开始时间" prop="StartTime">
          <a-input v-model="entity.StartTime" autocomplete="off" />
        </a-form-model-item>
        <a-form-model-item label="结束时间" prop="EndTime">
          <a-input v-model="entity.EndTime" autocomplete="off" />
        </a-form-model-item>

        <a-form-model-item label="工作时长" prop="WorkHour">
          <a-input-number id="WorkHourNumber" v-model="entity.WorkHour" :min="0" :max="24"/>
        </a-form-model-item>

        <a-form-model-item label="休息时长" prop="RestHour">
          <a-input-number id="RestHourNumber" v-model="entity.RestHour" :min="0" :max="24" defaultValue="0"/>
        </a-form-model-item>

        <a-form-model-item label="班次类型" prop="WorkingType">
          <a-radio-group v-model="entity.WorkingType">
            <a-radio value="上班">
              上班
            </a-radio>
            <a-radio value="加班">
              加班
            </a-radio>
          </a-radio-group>
        </a-form-model-item>
      </a-form-model>
    </a-spin>
  </a-modal>
</template>

<script>
export default {
  props: {
    afterSubmit: {
      type: Function,
      default: null
    },
    // eslint-disable-next-line vue/require-default-prop
    rowData: {
      type: Object
    }
  },
  watch: {
    rowData () {
      this.temp = this.rowData
    }
  },
  data () {
    return {
      temp: this.rowData,
      layout: {
        labelCol: { span: 5 },
        wrapperCol: { span: 18 }
      },
      visible: false,
      confirmLoading: false,
      entity: {},
      rules: {
        WorkingTimesName: [{ required: true, message: '必填' }],
        StartTime: [{ required: true, message: '必填' }],
        EndTime: [{ required: true, message: '必填' }],
        WorkHour: [{ required: true, message: '必填' }]
      }
    }
  },
  methods: {
    init () {
      this.visible = true
      this.entity = {}
      this.$nextTick(() => {
        this.$refs['form'].clearValidate()
      })
      this.entity.ParentWorkingTimesID = this.rowData.WorkingTimesID
    },
    openForm (id) {
      this.init()

      if (id) {
        this.$http.post('/Aps/Aps_WorkingTimes/GetTheData', { id: id }).then(resJson => {
          this.entity = resJson.Data
          // if (this.entity['Birthday']) {
          //  this.entity['Birthday'] = moment(this.entity['Birthday'])
          // }
        })
      }
    },
    handleSubmit () {
      this.$refs['form'].validate(valid => {
        if (!valid) {
          return
        }
        this.confirmLoading = true
        this.entity.Status = 1
        this.$http.post('/Aps/Aps_WorkingTimes/SaveData', this.entity).then(resJson => {
          this.confirmLoading = false

          if (resJson.Success) {
            this.$message.success('操作成功!')
            this.afterSubmit()
            this.visible = false
          } else {
            this.$message.error(resJson.Msg)
          }
        })
      })
    }
  }
}
</script>
